<?php
namespace app\admin\controller;
use app\service\repairService;
use think\facade\Db;
use think\facade\View;

class Notice extends Common{
    public function noticeList()
    {
        //根据当前登录账号查询物业公司
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $page = request()->has('page') ? request()->get('page') : 1;
        $noticelist = (new repairService())->noticeList($whe,$page);
        $number = count((new repairService())->noticeNumber($whe));
        View::assign('number',$number);
        if(!empty($number)){
            foreach($noticelist as $notice){
                //根据小区ID查询小区名称
                $notice['community_id'] = (new repairService())->communityIdName($notice['community_id']);
                $list[] = $notice;
            }
            View::assign('list',$list);
        }else{
            View::assign('list',$noticelist);
        }
        View::assign('noticelist',$noticelist);
        return View::fetch('notice_list');
    }
    public function noticeAdd()
    {
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $communitylist = (new repairService())->communityList($whe);
        View::assign('communitylist',$communitylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new repairService())->noticeAdd($data);
            if($result){
                $this->success('发布公告成功',url('noticeAdd'));
            }else{
                $this->error('发布公告失败',url('noticeAdd'));
            }
        }
        return View::fetch('notice_add');
    }
    public function noticeInfo()
    {
        $community_id = request()->get('community_id');
        //根据小区ID查询公告信息
        $noticeinfolist = (new repairService())->noticeInfoList($community_id);
        View::assign('noticeinfolist',$noticeinfolist);
        return View::fetch('notice_info');
    }

    public function noticedel()
    {
        $id=request()->get("id");

        
        $result = Db::name('notice')->delete($id);
        if($result){
            $this->success('删除成功',url('noticeList'));
        }else{
            $this->success('删除失败',url('noticeList'));
        }
    }
}